package com.kjj.org.service.impl;

import com.kjj.basic.service.impl.BaseServiceImpl;
import com.kjj.org.domain.Department;
import com.kjj.org.mapper.DepartmentMapper;
import com.kjj.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    //公共方法通过继承BaseServiceImpl实现
    //特有的接口方法需要自己实现


    @Override //我们的业务方法不是默认就能满足的了，所以要重写
    public void add(Department department) {
        department.setCreate_time(new Date()); //只是设置值
        super.add(department); //在xml中配置了保存完就返回id

        Long deptId = department.getId();
        //生成path
        String path = "";
        if (department.getParent()==null || department.getParent().getId()==null){
            //1 判断是否是顶级部门，如果是，自己生成  /id
            path = "/"+deptId;
        }else{
            //2 否则parentPath+/id
            Long pid = department.getParent().getId();
            String pPath = super.getById(pid).getPath();
            path = pPath+"/"+deptId;
        }

        department.setPath(path);
        department.setUpdate_time(new Date());
        super.update(department);
    }

    @Override
    public void update(Department department) {

        Long deptId = department.getId();
        //生成path
        String path = "";
        if (department.getParent()==null){
            //1 判断是否是顶级部门，如果是，自己生成  /id
            path = "/"+deptId;
        }else{
            //2 否则parentPath+/id
            Long pid = department.getParent().getId();
            String pPath = super.getById(pid).getPath();
            path = pPath+"/"+deptId;
        }
        department.setPath(path);
        department.setUpdate_time(new Date());

        super.update(department);
    }

    @Override
    public List<Department> getTree() {
        return departmentMapper.loadTree();
    }
}
